﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Enterprises.Wpf.Sample.ViewModel.Main
{
    /// <summary>
    /// RelayCommandDemoCtl ViewModel
    /// </summary>
    public partial class RelayCommandDemoCtlViewModel : ObservableObject
    {
        public RelayCommandDemoCtlViewModel()
        {
            // 1、 使用 ICommand RelayCommand
            IncrementCounterCommand = new RelayCommand(IncrementCounter);

            // 2、使用 AsyncRelayCommand
            LoadAsyncCommand = new AsyncRelayCommand<string>(DoLongTimeTaskAsync);
        }

        /// <summary>
        /// 1、 使用 ICommand
        /// </summary>
        public ICommand IncrementCounterCommand { get; }

        private void IncrementCounter() => Counter++;

        /// <summary>
        /// 2、使用 AsyncRelayCommand
        /// </summary>

        public IAsyncRelayCommand<string> LoadAsyncCommand { get; }

        /// <summary>
        /// Implements the logic for <see cref="LoadAsyncCommand"/>.
        /// </summary>
        /// <param name="time">模拟一个长时间运行异步任务</param>
        private async Task DoLongTimeTaskAsync(string time)
        {
            var deleyTime = string.IsNullOrEmpty(time) ? 5 : int.Parse(time);
            deleyTime = deleyTime * 1000;
            var text = await Task.Run(async () =>
            {
                await Task.Delay(deleyTime);
                return $"我执行花费了{time}秒异步事件任务";
            });

            Result = text;
        }

        /// <summary>
        /// 3、RelayCommand 特性简单使用.
        /// 它回自动生产InfoCommand 方法
        /// </summary>
        [RelayCommand]
        private void Info()
        {
            Growl.Info("我是RelayCommand特性使用，它回让事件定义更简单");
        }

        /// <summary>
        /// 4、RelayCommand 特性简单使用.
        /// 它回自动生产WarningCommand 方法
        /// </summary>
        [RelayCommand]
        private async Task Warning()
        {
            await Task.Delay(100);
            Growl.Warning("我是RelayCommand特性异步方法使用，注意看按钮效果");
        }

        
        /// <summary>
        /// 5、RelayCommand 带参数使用.
        /// </summary>
        [RelayCommand]
        private async Task Answer(string name)
        {
            await Task.Delay(100);
            Growl.Info(name+ " "+ UserName);
        }

        /// <summary>
        /// 6、RelayCommand 启用禁用按钮.
        /// </summary>
        [RelayCommand(CanExecute = nameof(IsEnabled))]
        private void HelloWord()
        {
            Growl.Info("Hello, World!");
        }

        [ObservableProperty]
        [NotifyCanExecuteChangedFor(nameof(HelloWordCommand))]
        private bool isEnabled;

        [ObservableProperty]
        private string result;

        [ObservableProperty]
        private string userName = "张三";

        private int counter;

        public int Counter
        {
            get => counter;
            private set => SetProperty(ref counter, value);
        }
    }
}
